package cn.hls.web.controller.biz;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hls.entity.Company;
import cn.hls.entity.User;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.CompanyService;
import cn.hls.web.controller.base.BaseController;

/**
 * @fileName: CompanyController.java
 * @package: cn.hls.web.controller.biz
 * @Description: 企业基本信息COntroller
 * @author: yinghui.zhang
 * @email: uuhui@163.com
 * @date: 2015-7-18
 */
@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController<Company> {
	@Resource(name = "companyService")
	private CompanyService companyService;
	
	 
	/**
	 * 表单
	 * 
	 * @param id 编号
	 * @param type 企业类型
	 * @param show 是否查看操作 true:查看操作 false:新增或修改操作
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/info")
	public ModelAndView info(Long id,Integer type, boolean show )
			throws Exception {
		Company entity = null;
		if (id != null) {
			entity = companyService.find(id);
		}
		if (show) {
			return new ModelAndView("company/show").addObject("entity", entity); // 查看页面
		} else {
		
			return new ModelAndView("company/info")
					.addObject("entity", entity)
					.addObject("type",type);// 新增、修改页面
		}

	}

	/**
	 * 添加操作
	 * 
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Msg add(Company entity) throws Exception {
		if(!companyService.exists(entity)){
			boolean flag = companyService.add(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(201);
			}
		}else{
			msg.setInfo("存在相同的全称或工商注册号信息!");
			msg.setStatus(201);
		}
		return msg;
	}

	 
	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/del")
	public Msg del(String ids) throws Exception {
		String[] tempIds = ids.split("\\,");
		companyService.delete(tempIds);
		return msg;
	}

	/**
	 * 修改操作
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Msg update(Company entity) throws Exception {
		try {
			boolean flag = companyService.update(entity);
			if (flag) {
				msg.setInfo("操作成功");
				msg.setStatus(200);
			} else {
				msg.setInfo("操作失败");
				msg.setStatus(201);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setInfo(e.getMessage());

		}
		return msg;
	}

	/**
	 * 分页查询
	 * 
	 * @param entity 查询的会友对象
	 * @param page 会友分页对象
	 * @param nodeIsLeaf 判断选择的节点是否分叶子节点
	 * @return PageInfo<Company>
	 * @throws Exception
	 */
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<Company> list(Company entity, PageInfo<Company> page)
			throws Exception {
		return companyService.loadCompanyInfo(entity, page);
	}
	
	@RequestMapping("/loadCompany")
	@ResponseBody
	public List<Company> loadCompany(Company entity,HttpServletRequest request)throws Exception{
		User user = (User) request.getSession().getAttribute("currentUser");
		Long carryCompanyId=user.getPerson().getCompanyId();//获取承运方企业编号
		if(carryCompanyId!=null){
			if(entity==null){
				entity=new Company();
			}
			entity.setCarryCompanyId(carryCompanyId);
		}
		return companyService.loadCompanyList(entity);
	}
	
	@RequestMapping("/loadCompanyOfShipper")
	@ResponseBody
	public List<Company>loadCompanyOfShipper(HttpServletRequest request,Company entity)throws Exception{
		User user = (User) request.getSession().getAttribute("currentUser");
		Long carryCompanyId=user.getPerson().getCompanyId();//获取承运方企业编号
		if(carryCompanyId!=null){
			if(entity==null){
				entity=new Company();
			}
			entity.setCarryCompanyId(carryCompanyId);
		}
		return companyService.loadCompanyOfShipper(entity);
	}
	
	 
	/**
	 * 跳转到承运企业视图
	 * @return
	 */
	@RequestMapping("/carryView")
	public ModelAndView carryView(HttpServletRequest request) {
		return new ModelAndView("/company/list").addObject("type", 1).addObject("searchAttribute",request.getParameter("pa"));

	}
	/**
	 * 跳转到托运企业视图
	 * @return ModelAndView
	 */
	@RequestMapping("/shipView")
	public ModelAndView shipView(){
		return new ModelAndView("/company/list").addObject("type",2); //type:企业类型  1－承运企业 2-托运企业
	}
}
